package com.qf.openapi.web.master.controller;

import com.github.pagehelper.PageInfo;
import com.qf.openapi.web.master.entity.AdminUser;
import com.qf.openapi.web.master.service.IAdminUserService;
import com.qf.openapi.web.master.vo.MessageVo;
import com.qf.openapi.web.master.vo.PageVo;
import com.qf.openapi.web.master.vo.TableVo;
import com.qf.openapi.web.master.vo.AdminVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/user")
public class AdminUserController {
    @Autowired
    private IAdminUserService iAdminUserService;

    @RequestMapping(value = "/login")
    @ResponseBody
    public MessageVo login(HttpSession session, @RequestBody AdminUser adminUser) {
        MessageVo messageVo = new MessageVo();
        Boolean login = iAdminUserService.getLogin(adminUser.getRealName(), adminUser.getPassword());
        String capchar = (String) session.getAttribute("captcha");
        if (login) {
            messageVo.setCode("1");
            session.setAttribute("adminUser", adminUser);
            messageVo.setData(adminUser);
            return messageVo;
        } else {
            messageVo.setCode("0");
            messageVo.setMessage("用户名或密码错误");
            return messageVo;
        }
    }

    @RequestMapping(value = "/getCode")
    @ResponseBody
    public MessageVo getCode(String code, HttpSession session) {

        MessageVo messageVo = new MessageVo();
        String capchar = (String) session.getAttribute("captcha");
        if(code == null && code.equals("")){
            messageVo.setCode("0");
            messageVo.setMessage("验证码不为空");
            return messageVo;
        }
        if (capchar.equalsIgnoreCase(code)) {
            messageVo.setCode("1");
            messageVo.setMessage("验证码正确");
            return messageVo;
        }else {
            messageVo.setCode("0");
            messageVo.setMessage("验证码错误");
            return messageVo;
        }
    }

    @RequestMapping(value = "/getAllUser")
    @ResponseBody
    public TableVo<AdminUser> getAllUser(PageVo<AdminUser> pageVo , String realName,String email,String status) {
        TableVo<AdminUser> tableVo = new TableVo<AdminUser>();
        PageInfo<AdminUser> byUsers = iAdminUserService.getByUsers(pageVo.getPage(), pageVo.getLimit(),realName,email,status);
        tableVo.setCode(0);
        tableVo.setMsg("ok");
        tableVo.setCount(byUsers.getTotal());
        tableVo.setData(byUsers.getList());
        return tableVo;
    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public MessageVo del(Integer[] ids) {
        MessageVo messageVo = new MessageVo();
        Boolean delUser = iAdminUserService.delUser(ids);
        if (delUser) {
            messageVo.setCode("1");
            messageVo.setStatus(true);
            messageVo.setMessage("删除成功");
            return messageVo;
        } else {
            messageVo.setCode("0");
            messageVo.setStatus(false);
            messageVo.setMessage("删除失败");
            return messageVo;
        }
    }

    @RequestMapping(value = "/add")
    @ResponseBody
    public MessageVo add(AdminUser adminUser) {
        MessageVo messageVo = new MessageVo();
        Boolean aBoolean = iAdminUserService.addUser(adminUser);
        if (aBoolean) {
            messageVo.setStatus(true);
            messageVo.setCode("1");
            messageVo.setMessage("添加成功");
            return messageVo;
        } else {
            messageVo.setStatus(false);
            messageVo.setMessage("添加失败");
            messageVo.setCode("0");
            return messageVo;
        }
    }
    @RequestMapping(value = "/update")
    @ResponseBody
    public MessageVo update(AdminVo adminVo) {
        MessageVo messageVo = new MessageVo();
        AdminUser adminUser = new AdminUser(adminVo.getId(),adminVo.getPassword(),adminVo.getEmail(),adminVo.getUsername(),adminVo.getStatus());
        Boolean aBoolean = iAdminUserService.updateUser(adminUser);
        if (aBoolean) {
            messageVo.setStatus(true);
            messageVo.setCode("1");
            messageVo.setMessage("修改成功");
            return messageVo;
        }else {
            messageVo.setStatus(false);
            messageVo.setCode("0");
            messageVo.setMessage("修改失败");
            return messageVo;
        }
    }
}
